package cn.itcast.controller;
import cn.itcast.constant.MessageConstant;
import cn.itcast.domain.CheckItem;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.service.CheckItemService;
import cn.itcast.utils.validation.servicees.InsertGroup;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/item")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean) {

        // 返回一个最大记录数 和 data集合
        PageResult pageResult = checkItemService.getpageQuery(pageBean);


        return pageResult;
    }
    @RequestMapping("/addData")
    public Result addData(@RequestBody CheckItem checkItem){

        try {
            checkItemService.addData(checkItem);
            return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result (false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }
    @RequestMapping("/deleteData")
    public Result deleteData(Integer id){

        try {
            checkItemService.deleteData(id);
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result (false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/editData")
    public Result editData(@RequestBody CheckItem checkItem){

        try {
            checkItemService.editData(checkItem);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result (false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findPage(){
        try {
            List<CheckItem> itemList = checkItemService.getAllList();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,itemList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }
    @RequestMapping("/valiFiled")
    public Result check(@RequestBody @Validated(InsertGroup.class) CheckItem checkItem, BindingResult bindingResult) {
            String message = "validated success !";
            System.out.println(checkItem);
            StringBuilder builder = new StringBuilder();
        if(bindingResult.hasErrors()){
            for (ObjectError error : bindingResult.getAllErrors()) {
                String defaultMessage = error.getDefaultMessage();
                builder.append(defaultMessage).append("!!!");
            }
            return new Result(false,builder.toString());
            }
            return new Result(true,message);
    }

}

